package com.shqcjd.preinstallmobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.api.ApiService;
import com.shqcjd.preinstallmobile.app.AppConstant;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallmobile.jpush.TagAliasUtils;
import com.shqcjd.preinstallmobile.model.ResponseData;
import com.shqcjd.preinstallmobile.model.pojo.LoginBean;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;
import com.shqcjd.preinstallmobile.util.PrefUtils;
import com.shqcjd.preinstallmobile.util.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_login)
    EditText etLogin;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_tourist_login)
    TextView tv_tourist_login;

    private Context mContext;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        mContext = this;
    }


    @OnClick({R.id.tv_login,R.id.tv_tourist_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                login();
                break;
            case R.id.tv_tourist_login:
                touristLogin();
                break;
        }

    }

    private void login() {
        String account = etLogin.getText().toString().trim();
        String password = etPwd.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.show(this,"请输入账号", Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.show(this,"请输入密码", Toast.LENGTH_SHORT);
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("loginName",account);
        map.put("password", password);
        map.put("application_code", "1");
        ApiService.login(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<LoginBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在登录...");
                    }

                    @Override
                    public void _onNext(ResponseData<LoginBean> retData) {
                        Logger.i("登录"+retData);
                        if (retData != null && retData.retCode == 10000) {
                            PrefUtils.setBoolean(mContext, PrefUtils.FIRST_LOGIN, true);
                            PrefUtils.setInt(mContext, PrefUtils.USER_ID, retData.data.id);
                            PrefUtils.setString(mContext, PrefUtils.USER_CODE, retData.data.user_code);
                            PrefUtils.setString(mContext, PrefUtils.USER_NAME, retData.data.user_name);
                            PrefUtils.setString(mContext, PrefUtils.PHONE_NUMBER, retData.data.phone_number);
                            PrefUtils.setString(mContext, PrefUtils.USER_ICON, retData.data.icon);
                            PrefUtils.setString(mContext, AppConstant.TOKEN_KEY, retData.data.token);
                            PrefUtils.setInt(mContext, PrefUtils.USER_TYPE, retData.data.user_type);
                            PrefUtils.setString(mContext, PrefUtils.USER_ACCOUNT, account);
                            PrefUtils.setString(mContext, PrefUtils.USER_PWD, password);
                            new TagAliasUtils().TagAliasUtils(retData.data.id+"");
                            startActivity(new Intent(LoginActivity.this,MainActivity.class));
                            finish();
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }


                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });



    }

    private void touristLogin(){
        Map<String, String> map = new HashMap<>();
        map.put("loginName","88888888");
        map.put("password", "123456");
        map.put("application_code", "1");
        ApiService.login(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<LoginBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在登录...");
                    }

                    @Override
                    public void _onNext(ResponseData<LoginBean> retData) {
                        Logger.i("登录"+retData);
                        if (retData != null && retData.retCode == 10000) {
                            PrefUtils.setBoolean(mContext, PrefUtils.FIRST_LOGIN, true);
                            PrefUtils.setInt(mContext, PrefUtils.USER_ID, retData.data.id);
                            PrefUtils.setString(mContext, PrefUtils.USER_CODE, retData.data.user_code);
                            PrefUtils.setString(mContext, PrefUtils.USER_NAME, retData.data.user_name);
                            PrefUtils.setString(mContext, PrefUtils.PHONE_NUMBER, retData.data.phone_number);
                            PrefUtils.setString(mContext, PrefUtils.USER_ICON, retData.data.icon);
                            PrefUtils.setString(mContext, AppConstant.TOKEN_KEY, retData.data.token);
                            PrefUtils.setInt(mContext, PrefUtils.USER_TYPE, retData.data.user_type);
                            PrefUtils.setString(mContext, PrefUtils.USER_ACCOUNT, "88888888");
                            PrefUtils.setString(mContext, PrefUtils.USER_PWD, "123456");
                            new TagAliasUtils().TagAliasUtils(retData.data.id+"");
                            startActivity(new Intent(LoginActivity.this,MainActivity.class));
                            finish();
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }


                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }
}
